package Servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BusinessDelegate.FacadeBusinessDelegate;
import EntitiesDTO.ItemCotizacionDTO;
import EntitiesDTO.RodamientoDTO;

/**
 * Servlet implementation class for Servlet: IniciarNuevaCotizacion
 * 
 */
public class IniciarNuevaCotizacion extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = (String) request.getParameter("action");

		if (action == null) {
			this.getAllRodamientos(request, response);
		}

		if (action != null && action.equals("Agregar")) {
			this.agregarItemCotizacion(request, response);
		}
		//
		// if (action != null && action.equals("GUARDAR ARTICULO")) {
		// this.guardarArticulo(request, response);
		// }

	}

	@SuppressWarnings("unchecked")
	private void agregarItemCotizacion(HttpServletRequest request,
			HttpServletResponse response) {

		HttpSession session = request.getSession(true);

		String rodamiento = request.getParameter("selectRodamiento");
		String cantidad = request.getParameter("cantRodamiento");

		List<ItemCotizacionDTO> itemCotizacion = (List<ItemCotizacionDTO>) session
				.getAttribute("itemscotizacion");

		if (itemCotizacion == null) {
			itemCotizacion = new ArrayList<ItemCotizacionDTO>();
		}

		ItemCotizacionDTO item = new ItemCotizacionDTO();
		try {
			item.setRodamiento(FacadeBusinessDelegate.getInstance()
					.getRodamientobyID(Integer.parseInt(rodamiento)));
			item.setCantidad(Integer.parseInt(cantidad));
			itemCotizacion.add(item);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		}

		session.setAttribute("itemscotizacion", itemCotizacion);
		RequestDispatcher dispatcher = request
				.getRequestDispatcher("./NuevaCotizacion.jsp");

		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void getAllRodamientos(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession(true);

		List<RodamientoDTO> rodamientos = null;
		try {
			rodamientos = FacadeBusinessDelegate.getInstance()
					.getAllRodamientos();
		} catch (NamingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		session.setAttribute("rodamientos", rodamientos);

		RequestDispatcher dispatcher = request
				.getRequestDispatcher("./NuevaCotizacion.jsp");

		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}